package com.hospital.pharmacy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hospital.login.service.LoginService;
import com.hospital.pharmacy.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping(value = "/medicine")
public class MedicineController {
    @Autowired
    MedicineService medicineService;
    @Autowired
    LoginService loginService;

    @RequestMapping(value = "/add",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String insertMed(@RequestBody String data){
        //Map<String,String>map=null;
        JSONObject jsonObject= JSON.parseObject(data);

        String num=jsonObject.getString("mdcNum");
        String name=jsonObject.getString("mdcName");
        String unit=jsonObject.getString("mdcUnit");
        String spec=jsonObject.getString("mdcSpec");
        String type=jsonObject.getString("mdcTypeId");
        String supplier=jsonObject.getString("mdcSupplierId");
        String company=jsonObject.getString("mdcCompany");
        String remark=jsonObject.getString("mdcRemark");


        return medicineService.insertMed(num,name,unit,spec,Integer.parseInt(type),Integer.parseInt(supplier),company,remark);
    }
    @RequestMapping(value = "/delete",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String deleteMed(@RequestBody String data){
        String mdcId=JSONObject.parseObject(data).getString("mdcId");
        return medicineService.deleteMed(Integer.parseInt(mdcId));

    }
   /* @RequestMapping(value = "/selectOne",produces = "application/json;charset=utf-8",method =RequestMethod.POST)
    @ResponseBody
    public String selectOne(@RequestParam("id")String id){
        return medicineService.getOneById(Integer.parseInt(id));
    }*/
    @RequestMapping(value = "/update",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody String data){
        String mdcId=JSONObject.parseObject(data).getString("mdcId");
        String mdcName=JSONObject.parseObject(data).getString("mdcName");
        String mdcRemark=JSONObject.parseObject(data).getString("mdcRemark");
        String mdcSpec=JSONObject.parseObject(data).getString("mdcSpec");
        String mdcUnit=JSONObject.parseObject(data).getString("mdcUnit");
        String mdcCompany=JSONObject.parseObject(data).getString("mdcCompany");
       return medicineService.update(Integer.parseInt(mdcId),mdcId,mdcName,mdcUnit,mdcSpec,mdcCompany,mdcRemark);

    }
    @RequestMapping(value = "/all",produces = "application/json;charset=utf-8",method =RequestMethod.GET)
    @ResponseBody
    public String selectAll(){
        return medicineService.getAllMed();
    }

    @RequestMapping(value = "/med_name",produces = "application/json;charset=utf-8",method =RequestMethod.POST)
    @ResponseBody
    public String selectByName(@RequestBody String data) {
        String mdcName=JSONObject.parseObject(data).getString("mdcName");
        return medicineService.getMedByMedName(mdcName);
    }


}
